<template>
  <div class="login-container">
    <el-card header="请先登录" class="login-card">
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item class="button-box">
          <el-button class="button" type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {},
    };
  },
  methods: {
    async login() {
      if (!this.model.username || !this.model.password) {
        this.$message({
          type: 'warning',
          message: '请输入用户名或密码',
        });
        return;
      }
      let res = await this.axios.post('login', this.model);
      console.log(res, 123);
      sessionStorage.token = res.data.token;
      sessionStorage.username = res.data.username;
      this.$router.push('/');
      this.$message({
        type: 'success',
        message: '登录成功',
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 20rem;
}

.button-box {
  display: flex;
  align-items: center;
  justify-content: center;
}
.button {
  width: 14rem;
}
</style>
